package com.fourhats.controller;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fourhats.services.PriceService;
import com.fourhats.services.dto.PriceDto;

@Controller
public class PriceController {

	@Autowired
	private PriceService priceService;
	
	@RequestMapping(value="/price/getPricesByItemAndPlaces", method=RequestMethod.POST)
	public @ResponseBody List<PriceDto> getPlaces(@RequestParam long itemId, @RequestParam("placesIds") Long[] placesIds){
		List<Long> ids = Arrays.asList(placesIds);
		return this.priceService.getEnabledPricesByItemAndPlace(itemId, ids);
	}
	
	@RequestMapping(value="/price/addPrice")
	public @ResponseBody String addPrice(@RequestParam long itemId, @RequestParam long placeId, @RequestParam Double value){
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		if(!(auth instanceof AnonymousAuthenticationToken)){
			this.priceService.addPrice(itemId, placeId, value);
			return "login_shown_for_ajax";	
		}
		return "login_hidden_for_ajax";
	}
}
